/*-----------------------------------------------------------------------
|                            FILE DESCRIPTION                           |
-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
  - File name     : zh_info_tool.h
  - Author        : liuzhihua
  - Update date   : 2022.6.28                  
  -	File Function : Tool boxes for informations
  - GB2312 Encoding
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               UPDATE NOTE                             |
-----------------------------------------------------------------------*/
/**
  * Update note:
  * ------------   ---------------   ----------------------------------
  *     Date            Author                       Note
  * ------------   ---------------   ----------------------------------
  *   2022.1.28       liuzhihua                   Create file
  *   2022.6.28       liuzhihua                   Update file             
***/

#ifndef _ZH_INFO_TOOL_H_
#define _ZH_INFO_TOOL_H_

#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "stdint.h" /* uint8_t ... */
#include "string.h" /* memcmp memcpy */
#include "math.h"   /* fabs */
#include "stdio.h"  /* memcpy printf */
#include "stdarg.h" /* va_list */
#include "stdlib.h" /* atol atof */
/*-----------------------------------------------------------------------
|                                DEFINES                                |
-----------------------------------------------------------------------*/
typedef struct Sentence_struct{

uint8_t* phead;	//帧头
uint8_t headSize;	//帧头长度
uint8_t separator;	//分隔符
uint8_t* ptail;	//帧尾
uint8_t tailSize;	//帧尾长度
uint8_t* pBuf;		//数据缓存区
uint8_t BufSize;	//缓冲区大小
uint8_t BufUsage;	  //分隔符出现的数量/缓存的使用量
uint8_t* pSrcAdr;	//检测地址
uint8_t SrcAdrSize;//检测缓存的大小
int headPos; 	//帧头位置
int tailPos;  //帧尾位置
}Sentence_t;
#define StCreate(head, sep, tail, pbuf, SrcAddress) \
  {                                                 \
    .phead = (uint8_t *)head,                       \
    .headSize = sizeof(head) - 1,                   \
    .separator = sep,                               \
    .ptail = (uint8_t *)tail,                       \
    .tailSize = sizeof(tail) - 1,                   \
    .pBuf = (uint8_t *)pbuf,                        \
    .BufSize = sizeof(pbuf),                        \
    .pSrcAdr = (uint8_t *)SrcAddress,               \
    .SrcAdrSize = sizeof(SrcAddress)                \
  }

#define StInit(pSt, head, sep, tail, pbuf, SrcAddress)           \
  do                                                             \
  {                                                              \
    Sentence_t St = StCreate(head, sep, tail, pbuf, SrcAddress); \
    memcpy(pSt, &St, sizeof(Sentence_t));                        \
  } while (0)

#define Format_Buffer_SIZE 128
extern char Format_Buffer[Format_Buffer_SIZE];
/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/
char* FormatToStr(const char* fmt,...);

uint8_t* St_IndexFind(Sentence_t *pSt, uint8_t *str, uint8_t Size, uint16_t index);
uint8_t St_Split(Sentence_t *pSt, uint8_t *str, uint8_t Size);
void St_SetAddress(Sentence_t *pSt, uint8_t* Address, uint8_t Size);
int St_MemSearch(uint8_t* str, uint8_t Size, uint8_t* phead, uint8_t headSize);

/**
 * @brief     获取分割后某段的数据长度
 * @param 	  Sentence句柄
 * @param	    下标
 * @retval    帧数据长度
**/
#define St_GetLen(pSt,index) strlen((const char *)&(pSt)->pSrcAdr[(pSt)->pBuf[index]])
/**
 * @brief     获取分割后的十六进制数据
 * @param 	  Sentence句柄
 * @param	    下标
 * @retval    字节指针
**/
#define St_GetHex(pSt,index) (uint8_t*)(&(pSt)->pSrcAdr[(pSt)->pBuf[index]])

/**
 * @brief     获取分割后的字符
 * @param 	  Sentence句柄
 * @param	    下标
 * @retval    字符指针
**/
#define St_GetStr(pSt,index) (char*)(&(pSt)->pSrcAdr[(pSt)->pBuf[index]])

/**
 * @brief     获取分割后的整型数据
 * @param 	  Sentence句柄
 * @param	    下标
 * @retval    整型数据
**/
#define St_GetInt(pSt,index) atol((const char*)(&(pSt)->pSrcAdr[(pSt)->pBuf[index]]))

/**
 * @brief     获取分割后的浮点型数据
 * @param 	  Sentence句柄
 * @param	    下标
 * @retval    浮点数据
**/
#define St_GetDob(pSt,index) atof((const char*)(&(pSt)->pSrcAdr[(pSt)->pBuf[index]]))

#define strstr_u8(buf,str) (uint8_t*)strstr((const char*) buf,(const char*) str)
/**
 * @brief     检测字符串是否出现帧头
 * @param 	  Sentence句柄
 * @param	  数字的序号
 * @retval    数据指针
**/
#define St_ISHEAD(pSt,str)  (memcmp(str,(pSt)->phead,(pSt)->headSize) == 0 ? 1 : 0)
#define St_FIND_HEAD(pSt)   ((pSt)->headPos = St_MemSearch((pSt)->pSrcAdr, (pSt)->SrcAdrSize, (pSt)->phead, (pSt)->headSize))
#define St_FIND_TAIL(pSt)   ((pSt)->tailPos = St_MemSearch((pSt)->pSrcAdr, (pSt)->SrcAdrSize, (pSt)->ptail, (pSt)->tailSize))
#define St_CHECK(pSt)       ((St_FIND_HEAD(pSt) >= 0 && St_FIND_TAIL(pSt) >= 0))
#define St_SPLIT(pSt)       (St_CHECK(pSt) && St_Split((pSt), (pSt)->pSrcAdr, (pSt)->SrcAdrSize) > 0)
/**
 * @brief     跳过指定字符
 * @param 	  字符串
 * @param 	  要跳过的字符
**/
#define Skip_Char(str, ch) \
  {                        \
    if (*str != NULL)      \
    {                      \
      while (**str == ch)  \
      {                    \
        str++;             \
      }                    \
    }                      \
  }

void Value_SetBit(unsigned int* pValue ,unsigned char bit, unsigned char Value);

/**
  * @brief  Set A DEC bit 
  * @param  aimValue :	Aim Value 
  * @param	bit	   :	position 0..9(0..4294967295)
  * @param	bitValue  :	position value
  * @retval None
  */
#define DEC_SetBit(aimValue,bit,bitValue) \
do{\
	unsigned int p = pow(10,bit);\
	aimValue -= (unsigned int)((aimValue % (unsigned int)(p*10))/p)*p;\
	aimValue += bitValue*p;\
} while(0)
/**
  * @brief  Get A DEC bit 
  * @param  aimValue :	Aim Value 
  * @param	bit	   :	position 0..9(0..4294967295)
  * @retval None
  */	
#define DEC_GetBit(aimValue,bit)\
do{\
	unsigned int p = pow(10,bit);\
	aimValue = (unsigned int)((aimValue % (unsigned int)(p*10))/p);\
}while(0)





#ifdef __cplusplus
	}
#endif
#endif
